/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function deleteUser() {

    if ($('input.IdUser:checked').val() != null)

    {
        document.getElementById('warning').innerHTML = "";
        document.getElementById("mainForm").action = "DeleteUser";
        document.getElementById("mainForm").submit();
        return true;
    }
    else {
        document.getElementById('warning').innerHTML = 'Please choose user';
        return false;
    }


}

function addUser() {
    document.getElementById("mainForm").action = "add_user.jsp";
    document.getElementById("mainForm").submit();
}


function editUser() {

    if ($('input.IdUser:checked').val() != null)
    {
        //alert($('input.IdUser:checked').val());
        document.getElementById('warning').innerHTML = "";
        document.getElementById("mainForm").action = "EditUser";
        document.getElementById("mainForm").submit();
        return true;
    }
    else {
        document.getElementById('warning').innerHTML = 'Please choose user';
        return false;
    }

}
function editUserConfirm() {
    checkFields();
    if (checkFields()) {
        document.getElementById("editForm").action = "EditUser";
        document.getElementById("editForm").submit();
    } else {
//        alert("Please enter correct data");
    }
}


function refreshUser() {
    document.getElementById("mainForm").action = "/Delta-war/refresh";
    document.getElementById("mainForm").submit();
}

function checkFind() {


    if ($('#keyword').val().match(/^[a-z]+$/i)) {
        // alert($('#keyword').val());
        $('#warningFind').html('');
        // alert('good');
        return true;
    }
    else {

        $('#warningFind').html('Please input user surname or name!');
        //  alert('Input user surname or name!');

        return false;
    }

}

function sendRequest(button) {

//    alert($("input[name=IdUser]:checked").map(
//            function() {
//                return this.value;
//            }).get().join("t"));
var self=$(button);

var buttonType=self.attr('name');
if(buttonType=="assignRole"){
    if ($('input.idRole:checked').val()!=null && $('input.IdUser:checked').val()!=null)
    {
        //alert('assignRole start');
        $.ajax({
            type: "GET",
            url: "AssignRole",
            data: "idRole=" + $('input.idRole:checked').val() + "&users_for_group=" + ($("input[name=IdUser]:checked").map(
                    function() {
                        return this.value;
                    }).get().join("t")),
            success: function(data) {
                if (data == "true") {
//                alert('success');
                    //window.location.reload();
                    document.getElementById('infoRole').innerHTML = 'Role successfully add';
                }
                
            }
        });


    }else{
        
        document.getElementById('warningRole').innerHTML = 'Please select at least one user and one role';
        //alert("finish check fields");

    }
        
    
}else{
    if ($('input.idGroup:checked').val() != null && $('input.IdUser:checked').val() != null)
    {
     //   alert('assignGroup start');
        $.ajax({
            type: "GET",
            url: "AssignUsers",
            data: "buttonType="+buttonType+"&idGroup=" + $('input.idGroup:checked').val() + "&users_for_group=" + ($("input[name=IdUser]:checked").map(
                    function() {
                        return this.value;
                    }).get().join("t")),
            success: function(data) {
                if (data == "true") {
//                alert('success');
                   // window.location.reload();
                   document.getElementById('infoGroup').innerHTML = 'Operation successfully completed';
                }
            }
        });


    } else {

            document.getElementById('warningGroup').innerHTML = 'Please select at least one user';

    }}
}

function refreshSort() {
    document.getElementById("sort").submit();
}

function cancel() {
    document.getElementById("editform").action = "userview";
    document.getElementById("editform").submit();
}
function checkFields() {
   alert("start check fields");
    if (
            document.getElementById("First_Name").value != "" ||
            document.getElementById("Last_Name").value != "" ||
            document.getElementById("E_mail").value != "" ||
            document.getElementById("Address").value != "")
    {
        document.getElementById('warning').innerHTML = 'Please fill all fields';
        return false;
    }
   // alert("finish check fields");
}
function checkInfo(){
    //alert('sijkopl');
    var f_name=checkFName();
    var l_name=checkLName();
    var address=checkAddress();
    var email=document.getElementById("e-mail").value;
    var temp=validateEmail(email);
    if(!f_name || !l_name || !address){
        document.getElementById('ok').disabled=true;
//        document.getElementById('warning').innerHTML='Fill all the fields';
    }
    else{
        document.getElementById('warning').innerHTML='';
        if(email=='' || temp){
            document.getElementById('ok').disabled=false;
            //alert('SDFGJKLERTY');
            document.getElementById('warningEmail').innerHTML='';
        }
        else{
            document.getElementById('ok').disabled=true;
            document.getElementById('warningEmail').innerHTML='Wrong email';
        }
    }
    
}
function validateEmail(str){
    var regex=new RegExp("^[A-Za-z0-9.%+\\-]+@[A-Za-z0-9.\\-]+\\.[A-Za-z]{2,4}$");
    return regex.test(str);
}

$(function(){
    $(document).on('submit','editform',function(e){
        e.preventDefault();
        var fname=document.getElementById("First_Name").value;
        var lname=document.getElementById("Last_Name").value;
        var address=document.getElementById("Address").value;
        if(fname!="" && lname!="" && address !=""){
            document.getElementById("warning").innerHTML='';
            $.post('EditUser',{
                First_name:fname,
                Last_name:lname,
                Email:document.getElementById("e-mail").value,
                Address:address
            },function(result){
                window.location.reload();
            })
        }
        else{
            document.getElementById("warning").innerHTML='Fill all the fields';
        }
    })
})

   